#!/user/bin/env python
# -*-coding:utf-8-*-
'''
@author: Patty
@project: automation_framework
@file: product_details.py
@time: 2019/8/15  11:08
@IDE: PyCharm 
'''

from util.db_util import get_mongodb_connection
import logging

"""获取产业链"""
def get_industry_chain(type):
    con = get_mongodb_connection()
    my_db = con.opt
    my_set = my_db.supply_chain_relation
    results_list = []
    if type == 'out_bound':
        mongo_db = my_set.find(
            {"prime.szh": "乘用车", "related.rtyp": "T", "related.level": -1, "related.rdegree": {"$in": [3, 4]}},
            {"related.name.szh": 1})
        results = list(mongo_db)
        for szh in results:
            results_list.append(szh['related']['name']['szh'])
    elif type == 'in_bound':
        mongo_db = my_set.find(
            {"related.name.szh": "乘用车", "ptyp": "M", "related.level": 1, "related.rdegree": {"$in": [3, 4]}}, {
                "prime.szh": 1})
        results = list(mongo_db)
        for szh in results:
            results_list.append(szh['prime']['szh'])
    try:
        result = results_list[:]
        return result
    except Exception as e:
        logging.error("no result retrieved")
        return None
